package com.hongdee.insurance.commission.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.EntityUtils;
import com.hongdee.common.util.StringUtil;
import com.hongdee.insurance.Constant;
import com.hongdee.insurance.commission.entity.TInsuranceCommission;
import com.hongdee.insurance.commission.service.ITCommissionNewService;
import com.hongdee.insurance.commission.service.ITCommissionSrevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Mr. Xu on 2017/4/25.
 * 保险公司佣金信息
 */
@RestController
@RequestMapping("/insurance/commission")
public class TCommissionController {
    @Autowired
    private ITCommissionSrevice itCommissionSrevice;
    @Autowired
    private ITCommissionNewService commissionNewService;
    /**
     * 新增或修改佣金
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity Save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody Map<String,Object> data) throws Exception {
        Map<String,Object> insuranceMap= (Map<String, Object>) data.get("insurance");

        TInsuranceCommission insuranceCommission = (TInsuranceCommission) JSONArray.toJavaObject((JSON) JSONArray.toJSON(insuranceMap),TInsuranceCommission.class);
        //保存实体
        List<String> carCompanyList=new ArrayList<>();
        if(data.get("carCompany") instanceof ArrayList){//判断是多选还是单选
            carCompanyList=(List<String>) data.get("carCompany");
        }else{
            carCompanyList.add(String.valueOf(data.get("carCompany")));
        }
        EntityUtils.valid(insuranceCommission);
        this.itCommissionSrevice.saveOrUpdate(insuranceCommission,uid,carCompanyList);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("佣金保存成功！");
        return res;
    }

    /**
     * 单交 单商 交商共保后的新增
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/save/new")
    @ResponseBody
    public ResultEntity saveNew(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestBody Map<String,Object> data) throws Exception {
        Map<String,Object> insuranceMap= (Map<String, Object>) data.get("insurance");
        //保存实体
        List<String> carCompanyList=new ArrayList<>();
        if(data.get("carCompany") instanceof ArrayList){//判断是多选还是单选
            carCompanyList=(List<String>) data.get("carCompany");
        }else{
            carCompanyList.add(String.valueOf(data.get("carCompany")));
        }
        this.commissionNewService.saveInsuranceCommission(insuranceMap,carCompanyList,uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("佣金保存成功！");
        return res;
    }

    /**
     * 更新佣金接口
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/update")
    @ResponseBody
    public ResultEntity update(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestBody TInsuranceCommission data) throws Exception {
        this.commissionNewService.updateInsuranceCommission(data,uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("佣金保存成功！");
        return res;
    }

    /**
     * 佣金分页查询
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestBody Map data) throws Exception {
        Map map = itCommissionSrevice.page(uid,start, limit,data);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }


    /**
     * 佣金分页查询
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/page/new")
    @ResponseBody
    public Map pageNew(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestBody Map data) throws Exception {
        Map map = commissionNewService.page(uid,start, limit,data);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 佣金展示板
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/applyUnit/page")
    @ResponseBody
    public Map applyUnitPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestBody Map data) throws Exception {
        Map map = itCommissionSrevice.applyUnitPage(uid,start, limit,data);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 佣金明细
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择保险公司！");
        }
        Map map = itCommissionSrevice.info(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

    /**
     * 获得车辆销售公司
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/carCompanys")
    @ResponseBody
    public ResultEntity carCompanys(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    @RequestParam(name = "area", defaultValue = "") String area) throws Exception {
        if("".equals(area)){
            throw new Exception("请选择区域信息!!");
        }
        ResultEntity res=new ResultEntity();
        List<Map<String,Object>> data=this.itCommissionSrevice.getCarCompanys(area);
        res.setData(data);
        res.setSuccess(true);
        return res;
    }

    @RequestMapping("/getCarCompanys")
    @ResponseBody
    public ResultEntity getCarCompanys(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        ResultEntity res=new ResultEntity();
        List<Map<String,Object>> data=this.itCommissionSrevice.getCarCompanysToBill();
        res.setData(data);
        res.setSuccess(true);
        return res;
    }

    /**
     * 根据报价单 获得对应公司的佣金信息
     * @param uid
     * @param token
     * @param quotationId
     * @return
     * @throws Exception
     */
    @RequestMapping("/getInsuranceCommission")
    @ResponseBody
    public ResultEntity getInsuranceCommission(@RequestParam(name = "uid", defaultValue = "") String uid,
                                               @RequestParam(name = "token", defaultValue = "") String token,
                                               @RequestParam(name = "time", defaultValue = "") String time,
                                               @RequestParam(name = "quotationId", defaultValue = "") String quotationId) throws Exception {
        ResultEntity res=new ResultEntity();
        Map<String,Object> data=this.itCommissionSrevice.getCarInsuranceCommissionByIdToBill(quotationId,time,uid,"");
        res.setMsg("");
        res.setData(data);
        res.setSuccess(true);
        return res;
    }

    /**
     * 佣金审核确认
     * @param uid
     * @param token
     * @param data
     * @return
     */
    @RequestMapping("/confim")
    @ResponseBody
    public ResultEntity confim(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestParam(name = "type", defaultValue = "") String type,
                               @RequestParam(name = "remark", defaultValue = "") String remark,
                               @RequestBody List<Map> data) throws Exception {
        ResultEntity res=new ResultEntity();
        this.itCommissionSrevice.examCommissonStatus(data,type,uid,remark);
        res.setSuccess(true);
        res.setMsg("佣金审核成功！！");
        return res;
    }

    @RequestMapping("/confim/list")
    @ResponseBody
    public ResultEntity confimList(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                   @RequestParam(name = "id",defaultValue = "")String id) throws Exception {
        if("".equals(id)){
            throw new Exception("请选择佣金信息!!");
        }
        ResultEntity res=new ResultEntity();
        List<Map<String,Object>> lists=this.itCommissionSrevice.findExalLogs(id);
        res.setSuccess(true);
        res.setData(lists);
        res.setMsg("查询成功!!");
        return res;
    }


    /**
     * 停用
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/disable")
    @ResponseBody
    public ResultEntity disable(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestParam(name = "id",defaultValue = "")String id) throws Exception {
        return this.disableEnable(id,String.valueOf(Constant.COMMISSION_STATUS.init.ordinal()));
    }

    /**
     * 启用
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/enable")
    @ResponseBody
    public ResultEntity enable(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestParam(name = "id",defaultValue = "")String id) throws Exception {
        List<TInsuranceCommission> list=new ArrayList<>();
        list.add(this.itCommissionSrevice.getInfo(id));
        this.commissionNewService.hisDateCompare(list);
        return this.disableEnable(id,String.valueOf(Constant.COMMISSION_STATUS.argee.ordinal()));
    }

    private ResultEntity disableEnable(String id,String status) throws Exception {
        if("".equals(id)){
            throw new Exception("请选择佣金信息!!");
        }
        ResultEntity res=new ResultEntity();
        this.itCommissionSrevice.enableAndDisableCommission(id,status);
        res.setSuccess(true);
        res.setMsg((status.equals(String.valueOf(Constant.COMMISSION_STATUS.init.ordinal()))?"停用":"启用")+"成功!!");
        return res;
    }
}
